//
// Created by ziya on 2020/8/30.
//

#ifndef CPP_MEMORY_POOL_MEMORYCHUNK_H
#define CPP_MEMORY_POOL_MEMORYCHUNK_H

#include "common.h"
#include "MemoryCell.h"

class MemoryChunk {

private:
    string  m_filename;
    int     m_line;

private:
    // 实际申请的内存大小
    int     m_size;

    // 内存对齐
    int     m_align_size;

    // 该chunk由多少cell
    int     m_max_cell_num;

    // 数据真实存放的地方
    char*   m_data;

    // 该chunk剩余的cell
    int     m_remain_cell_num;

private:
    list<MemoryCell *> m_available_table;
    list<MemoryCell *> m_used_table;

public:
    MemoryChunk();
    MemoryChunk(int size);
    MemoryChunk(int size, string filename, int line);
    ~MemoryChunk();

public:
    int getSize();
    string* getFilename();
    int getLine();

    list<MemoryCell *>* getAvailableTable();
    list<MemoryCell *>* getUsedTable();

    void printAvailableTable();
    void printUsedTable();

    MemoryChunk* incRemainCellNum(int step);
    MemoryChunk* decRemainCellNum(int step);

    void freeAvailableTable();
    void freeUsedTable();

public:
    MemoryCell* alloc(int size);

private:
    MemoryCell* real_malloc(MemoryCell *available_cell, uint cell_num);

};


#endif //CPP_MEMORY_POOL_MEMORYCHUNK_H
